package com.bindapp.activity;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;

import com.bindapp.model.LoginModel;
import com.bindapp.util.AppManager;
import com.bindapp.util.PreferencesUtil;
import com.bindapp.util.Static;
import com.yanzhenjie.permission.Action;
import com.yanzhenjie.permission.AndPermission;

import java.util.List;

/**
 * Created by tom055 on 2018/6/23.
 */

public class LoginActivity extends AppCompatActivity {
    private LoginModel model;
    private static String[] PERMISSIONS_STORAGE = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.CAMERA,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.INTERNET,
            Manifest.permission.ACCESS_NETWORK_STATE,
            Manifest.permission.CALL_PHONE

    };
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            Intent intent=new Intent(LoginActivity.this,TabActivity.class);
            startActivity(intent);
            AppManager.addActivity(LoginActivity.this);
            finish();
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        model=new LoginModel(LoginActivity.this);
        setContentView(model.getBinding().getRoot());
        validePermissions();
        AppManager.addActivity(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        AppManager.finishActivity(this);
    }

    private void validePermissions(){
        if(AndPermission.hasPermissions(LoginActivity.this,PERMISSIONS_STORAGE)){
            Log.e("Mytext","hasPermissions");
            //openapk();
            if(!PreferencesUtil.getInstance(LoginActivity.this).getCookie().equals("")){
                handler.sendMessage(new Message());
            }

        }else{
            AndPermission.with(LoginActivity.this)
                    .runtime()
                    .permission(PERMISSIONS_STORAGE)
                    .onGranted(new Action<List<String>>() {
                        @Override
                        public void onAction(List<String> data) {
                            Log.e("Mytext","onGranted");
                            if(!PreferencesUtil.getInstance(LoginActivity.this).getCookie().equals("")){
                                handler.sendMessage(new Message());
                            }
                        }
                    })
                    .onDenied(new Action<List<String>>() {
                        @Override
                        public void onAction(List<String> data) {
                            Log.e("Mytext","onDenied");
                            finish();
                        }
                    })
                    .start();
        }
        //1.udp网络请求做了并发请求封装。2.登陆页面做完 接口已接通。3注册页面做完接口还木有，4首页第一个tab页面已经好了。需要细调,还木有接口,5.第二个tab已经画好
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
            Static.myLog("按下了back键   onKeyDown()");
            finish();
            return false;
        }else {
            return super.onKeyDown(keyCode, event);
        }

    }

}
